<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form" lay-filter="userForm" >
    <div class="layui-form-item">
        <label class="layui-form-label required">账号</label>
        <div class="layui-input-block">
            <input type="text" name="username" lay-verify="required" readonly lay-reqtext="账号不能为空" placeholder="账号不能为空" value="" class="layui-input">

        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label required">密码</label>
        <div class="layui-input-block">
            <input type="password" name="password" lay-reqtext="密码不能为空" placeholder="请输入密码。密码为空，则不修改" value="" class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label required">所属角色</label>
        <div class="layui-input-block">
            <input type="text" name="roleName" id="roleName" ts-selected="" readonly lay-verify="required" lay-reqtext="名称不能为空" placeholder="名称不能为空" value="" class="layui-input">
            <input type="hidden" name="roleId" id="roleId" />
        </div>
    </div>
    <input type="hidden" name="adminId" />

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>

        </div>
    </div>

</div>

<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/js/lay-config.js?v=1.0.4" charset="utf-8"></script>

<script>
    layui.use(['table', 'form', 'tableSelect','treeTable'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            tableSelect = layui.tableSelect,
            $ = layui.$;
        var treeTable = layui.treeTable;

        tableSelect.render({
            elem: '#roleName',
            checkedKey: 'roleId',
            table: {
                url: '/role/jsonList',
                cols: [ [
                    {type: 'radio' },
                    {field: 'roleId', title: 'ID'},
                    {field: 'roleName', title: '角色名称'},
                    {field: 'roleContent', title: '角色权限'}
                ]]
            },
            done: function (elem, data) {
                var NEWJSON = []
                layui.each(data.data, function (index, item) {
                    NEWJSON.push(item.roleName)
                })
                elem.val(NEWJSON.join(","))
                $('#roleId').val(data.data[0].roleId)
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var formData = JSON.stringify(data.field);
            parent.layer.load();
            $.ajax({
                type: "POST",
                url: "/admin/userEdit",
                data: data.field,
                async: true,
                dataType: "json",
                success: function(data) {
                    parent.layer.closeAll('loading');

                    if(data.code == 1) {
                        parent.layer.msg(data.data);

                        parent.layui.table.reload("currentTableId");

                        // 关闭弹出层
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    }
                    else
                    {
                        parent.layer.alert(data.data,{icon:2});
                    }

                },
                error: function(e) {
                    layer.closeAll('loading');
                    parent.layer.alert(e);
                }
            });

            return false;
        });


        //加载表彰修改内容
        $.ajax({
            type: "GET",
            url: "/admin/getUserById?id=" + [[${id}]],
            data: "",
            async: true,
            dataType: "json",
            success: function(data) {
                form.val("userForm", data);
                $("#roleName").attr("ts-selected",data.roleId)
            },
            error: function(e) {
                parent.layer.alert(e);
            }
        });

    });
</script>
</body>
</html>